//
//  AllMemberViewController.m
//  Random
//
//  Created by 宜凯门-DKang on 2017/5/27.
//  Copyright © 2017年 ucanmax. All rights reserved.
//

#import "AllMemberViewController.h"
#define Key @"random"
#define PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]
@interface AllMemberViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *memberTableView;
@property (retain, nonatomic) NSMutableArray *listArray;
@end

@implementation AllMemberViewController

- (NSMutableArray *)listArray {
    if(!_listArray){
        _listArray= [NSMutableArray new];
    }
    return _listArray;
}
- (NSString *)getFilePath {
    return [PATH stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",Key]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"成员列表";
    self.memberTableView.tableFooterView = [UIView new];
    NSString *filePath = [PATH stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",Key]];
    self.listArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
    [self.memberTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.textLabel.text = self.listArray[indexPath.row];
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"Delete";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 从数据源中删除
    [self.listArray removeObjectAtIndex:indexPath.row];
    BOOL isWrite = [self.listArray writeToFile:[self getFilePath] atomically:YES];
    if(isWrite){
        // 从列表中删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
}
@end
